# 在上一题的基础上进行扩展，用户可以随意输入需要显示的行数。如输入13:21打印第13行到第21行，输入:21打印前21行，输入21:则打印从第21行开始到文件结尾所有内容


def func():
    file_path = input("请输入要打开的文件路径：")
    show_lines = input("请输入要显示的行数：")
    if show_lines.strip() == ":":
        # 显示所有行数
        begin = "1"
        end = "-1"
    (begin, end) = show_lines.split(":")
    if begin == " ":
        begin = "1"
    if end == "":
        end = "-1"

    if begin == "1" and end == "-1":
        prompt = "的全文"
    elif begin == "1":
        prompt = "从开始到%s" % end
    elif end == "-1":
        prompt = "从%s到结束" %begin
    else:
        prompt = "从第%s行到第%s行" % (begin, end)
    print("\n文件%s%s的内容如下：\n" % (file_path, prompt))

    begin = int(begin) - 1
    end = int(end)
    lines = end - begin

    file = open(file_path)

    for i in range(begin):
        file.readline() #用于消耗掉begin之前的内容
    if lines < 0:
        print(file.read())
    else:
        for j in range(lines):
            print(file.readline(), end = "")
    file.close()


if __name__ == "__main__":
    func()